package com.company.common.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.SimpleDateFormatSerializer;

import java.sql.Timestamp;
import java.util.Date;



public class JsonTools
{
    private static final SerializeConfig serializeConfig = new SerializeConfig();
    
    static
    {
        serializeConfig.put(Date.class, new SimpleDateFormatSerializer("yyyy-MM-dd HH:mm:ss"));
        serializeConfig.put(Timestamp.class, new SimpleDateFormatSerializer("yyyy-MM-dd HH:mm:ss"));
    }
    
    public static String getJsonFromObject(Object req)
    {
        return JSON.toJSONString(req, serializeConfig,SerializerFeature.WriteMapNullValue, SerializerFeature.DisableCircularReferenceDetect);
    }
    
    public static String toJson(Object obj)
    {
        return JSON.toJSONString(obj, serializeConfig,SerializerFeature.WriteMapNullValue, SerializerFeature.DisableCircularReferenceDetect);
    }
    
    public static <T> T fromJson(String str, Class<T> clazz)
    {
        return JSON.parseObject(str, clazz);
    }
    
    public static <T> T fromResourceJson(String str, Class<T> clazz)
    {
        return JSON.parseObject(str, clazz);
    }

}